package com.svo.weitu.model.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.svo.weitu.model.entity.MenuEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

public class ResData {
//	private Context context;
	private DbHelper dbHelper;
	private String tableName = "res";
	public ResData(Context context) {
//		this.context = context;
		dbHelper = DbHelper.getInstance(context);
	}
	public long insert(MenuEntity entity) {
		return insert(entity.getId(),entity.getTitle(), entity.getUri(), entity.getType(),entity.getUrl());
	}
	
	public long insert(int id,String name,String picUri,int type,String url) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("picUri", picUri);
		values.put("type", type);
		values.put("_id", id);
		values.put("url", url);
		return db.insert(tableName, null, values);
	}
	public List<MenuEntity> queryAll() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from "+tableName;
		Cursor cursor = db.rawQuery(sql,null);
		List<MenuEntity> entities = new ArrayList<MenuEntity>();
		MenuEntity entity;
		while (cursor.moveToNext()) {
			entity = new MenuEntity();
			entity.setTitle(cursor.getString(cursor.getColumnIndex("name")));
			entity.setUri(cursor.getString(cursor.getColumnIndex("picUri")));
			entity.setType(cursor.getInt(cursor.getColumnIndex("type")));
			entity.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			entity.setUrl(cursor.getString(cursor.getColumnIndex("url")));
			entities.add(entity);
		}
		return entities;
	}
	public void insert(List<MenuEntity> entities) {
		for (MenuEntity menuEntity : entities) {
			insert(menuEntity);
		}
	}
	/**
	 * 清除表内容
	 */
	public void clear() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from res");
	}
	
	//////////////////////////////////order表////////////////
	public Vector<Integer> getSubIds() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select _id from sub";
		Cursor cursor = db.rawQuery(sql,null);
		Vector<Integer> ids = new Vector<Integer>();
		while (cursor.moveToNext()) {
			ids.add(cursor.getInt(cursor.getColumnIndex("_id")));
		}
		return ids;
	}
	public void insertSub(List<MenuEntity> lists) {
		tableName = "sub";
		insert(lists);
	}
	public List<MenuEntity> querySubAll() {
		tableName = "sub";
		List<MenuEntity> list = queryAll();
		/*MenuEntity entity = new MenuEntity();
		entity.setId(2);
		entity.setTitle("最新");
		list.add(0,entity);
		entity = new MenuEntity();
		entity.setId(3);
		entity.setTitle("最热");
		list.add(0,entity);*/
		return list;
	}
	public void subDelete(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from sub where _id="+id);
	}
	public void subRes(int id) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from res where _id = "+id;
		Cursor cursor = db.rawQuery(sql,null);
		MenuEntity entity = null;
		if (cursor.moveToNext()) {
			entity = new MenuEntity();
			entity.setTitle(cursor.getString(cursor.getColumnIndex("name")));
			entity.setUri(cursor.getString(cursor.getColumnIndex("picUri")));
			entity.setType(cursor.getInt(cursor.getColumnIndex("type")));
			entity.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			entity.setUrl(cursor.getString(cursor.getColumnIndex("url")));
			tableName = "sub";
			insert(entity);
		}
	}
	/*
	 * 资源ID与资源名的键值对
	 */
	public HashMap<Integer, String> queryMap() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from res";
		Cursor cursor = db.rawQuery(sql,null);
		HashMap<Integer, String> resMap = new HashMap<Integer, String>();
		while (cursor.moveToNext()) {
			resMap.put(cursor.getInt(cursor.getColumnIndex("_id")), cursor.getString(cursor.getColumnIndex("name")));
		}
		return resMap;
	}
}
